package cn.edu.hit.selflearning;

import cn.edu.hit.core.Document;
import cn.edu.hit.core.db.*;
import cn.edu.hit.kg.data.GraphData;
import cn.edu.hit.kg.data.Node;
import net.sf.json.JSONObject;

import java.util.*;
import java.util.stream.Collectors;

public class GetPort {
    static DataConnection connection = DataConnectionLoader.getInstance();
    static DataReader reader = connection.getReader();
    static DBReadHelper dbHelper = new DBReadHelper(reader);

    public static void main(String[] args) {
        GraphData graphData = GraphData.getInstance();

//        DataConnection connection = DataConnectionLoader.getInstance();
//        DataReader reader = connection.getReader();
//        DBReadHelper dbHelper = new DBReadHelper(reader);


        List<Long> faultIds = graphData.getByLabel("Fault").map(fault -> fault.id()).collect(Collectors.toList());


        Map<Long, List<String>> fstMap = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();

        for (int i = 0; i < faultIds.size(); i++) {

            Long faultId = faultIds.get(i);
            System.out.println("FaultId : " + faultId);

            List<Node> faultList2 = graphData.getAllSubNodes(faultId).collect(Collectors.toList());

            mapList = faultList2.stream()
                    .filter(node -> node.hasLabel("Feature"))
                    .map(node -> {
                        Map<String, Object> nodeMap = new HashMap<>();
                        nodeMap.put("FaultId", faultId);
                        nodeMap.put("FeatureId", node.id());
                        nodeMap.put("name", node.asMap().get("name"));
                        return nodeMap;
                    }).collect(Collectors.toList());

//            System.out.println(mapList.toString());

            List<String> portNames = new ArrayList<>();
            for (int ij = 0; ij < mapList.size(); ij++) {
                String featureName = JSONObject.fromObject(mapList.get(ij)).getString("name");
                List<Document> channelList = reader.queryByTemplate(Filters.eq("signalType", featureName));
                for (int j = 0; j < channelList.size(); j++) {
                    String channelIdStr = channelList.get(j).getString("CombineChanenels");
                    String[] IdArray = channelIdStr.substring(1, channelIdStr.length() - 1).split(",");
                    for (int k = 0; k < IdArray.length; k++) {
                        String portId = IdArray[k];
                        if (portId.length() > 5) {
                            Document portDocunment = dbHelper.getById(portId.substring(1, portId.length() - 1));
                            if (portDocunment != null) {
                                String portName = portDocunment.getString("dataSourceArgs");
                                boolean hasport = false;
                                for (int l = 0; l < portNames.size(); l++) {
                                    if (portNames.get(l).equals(portName)) {
                                        hasport = true;
                                    }
                                }
                                if (!hasport) {
                                    portNames.add(portName);
                                    System.out.println(portName);
                                }
                            }
                        }
                    }
                }
            }
            fstMap.put(faultId, portNames);
        }
        System.out.println("finished");

        System.out.println(fstMap.toString());

    }
}
